//
//  BaseViewController.m
//  MenkNav
//
//  Created by Burn on 2019/6/14.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "BaseViewController.h"
#import "Macro.h"

@interface BaseViewController ()

@property(strong,nonatomic) UIView *left;
@property(strong,nonatomic) UIView *mid;
@property(strong,nonatomic) UIView *right;

@property (nonatomic, assign) CGRect playerFrame;   //记录播放器原始frame

@end

static CGFloat AnimationDuration = 0.3;     //旋转动画执行时间

@implementation BaseViewController

#pragma mark - HomeIndicatorAutoHidden
- (BOOL)prefersHomeIndicatorAutoHidden{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleDeviceOrientationChange:)name:UIDeviceOrientationDidChangeNotification object:nil];
}


- (UIStatusBarStyle)preferredStatusBarStyle{
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    } else {
        return UIStatusBarStyleDefault;
    }
}


- (UIButton *)backBTN{
    if (!_backBTN) {
        UIImage *image = [UIImage imageNamed:@"backBTN"];
        _backBTN = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBTN setImage:image forState:UIControlStateNormal];
//        _backBTN.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        _backBTN.frame = CGRectMake(0, 0, 50, 50);
//        _backBTN.backgroundColor = [UIColor redColor];
        [_backBTN setImageEdgeInsets:UIEdgeInsetsMake(-25, -25, 0, 0)];
        [_backBTN addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];

    }
    return _backBTN;
}

- (void)setNavTitle:(NSString *)navTitle{
    _navTitle = navTitle;
    _navTitle = [_navTitle stringByReplacingOccurrencesOfString:@" " withString:@"\n"];
    
}






#pragma mark - 全屏相关Begin
- (void)setPlayerVC:(BOOL)playerVC{
    _playerVC = playerVC;
    if (playerVC) {
        //开启和监听 设备旋转的通知
        if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
            [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        }
//        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleDeviceOrientationChange:)name:UIDeviceOrientationDidChangeNotification object:nil];
    }
}

- (void)setPlayerView:(UIView *)playerView{
    _playerView = playerView;
    self.playerFrame = self.playerView.frame;
}

//设备方向改变的处理
- (void)handleDeviceOrientationChange:(NSNotification *)notification{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    switch (deviceOrientation) {
        case UIDeviceOrientationPortrait:
            [self screenChangeOrientation];
            break;
        case UIDeviceOrientationLandscapeLeft:
            if (self.isFullScreen) {
//                [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
            }else{
//                self.videoView.small = NO;
//                self.videoView.fullScreen.selected = YES;
                
                [self screenChangeOrientation];
            }
            
            DLOG(@"屏幕向左横置");
            break;
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"UIDeviceOrientationLandscapeRight");
            if (self.isFullScreen) {
//                [self interfaceOrientation:UIInterfaceOrientationLandscapeLeft];
                NSLog(@"UIDeviceOrientationLandscapeRight - isFullScreen");
                
            }else{
//                self.videoView.fullScreen.selected = YES;
//                self.videoView.small = NO;
                [self screenChangeOrientation];
            }
            
            DLOG(@"屏幕向右橫置");
            break;

        default:
            DLOG(@"无法辨识");
            break;
    }
}


- (void)fullScreenAction{
    
    if (!self.isPlayerVC) { return; }
    
    
    if (self.isFullScreen) {//如果是全屏，点击按钮进入小屏状态
//        [self changeToOriginalFrame];
        
        NSNumber *value = [NSNumber numberWithInt:UIDeviceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        self.isFullScreen = NO;
        self.playerView.frame = self.playerFrame;
    } else {//不是全屏，点击按钮进入全屏状态   进入全屏
//        [self changeToFullScreen];
        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        if (orientation == UIDeviceOrientationLandscapeRight) {     // 这里取反是 因为手机往左边转话屏应该是往右转
            [self setFullScreen:UIInterfaceOrientationLandscapeLeft];
        } else {
            [self setFullScreen:UIInterfaceOrientationLandscapeRight];
        }
        
        self.isFullScreen = YES;
    }
    
}


- (void)screenChangeOrientation{
    
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
        self.isFullScreen = NO;
        self.playerView.frame = self.playerFrame;
    }else{
        self.playerView.frame = self.view.frame;
        self.isFullScreen = YES;
    }
    
    
}





//最后在dealloc中移除通知 和结束设备旋转的通知
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];

}
//  自动旋转关闭
- (BOOL)shouldAutorotate {
    return YES;
}
//  支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
}
#pragma mark - 全屏相关End



#pragma mark 设置是否全屏播放
- (void)setFullScreen:(UIInterfaceOrientation)orientation {
    
    UIInterfaceOrientation orientation1 = [[UIApplication sharedApplication] statusBarOrientation];
    NSLog(@"%ld",[UIDevice currentDevice].orientation);
    if (orientation1 == UIDeviceOrientationPortrait) {
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        NSLog(@"上 =%@== screen%@=",NSStringFromCGRect(self.view.frame),NSStringFromCGRect([UIScreen mainScreen].bounds));
    }else{
        NSNumber *value = [NSNumber numberWithInt:UIDeviceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        
        NSLog(@"下 =%@ == screen%@=",NSStringFromCGRect(self.view.frame),NSStringFromCGRect([UIScreen mainScreen].bounds));
        
    }
    
    
//    [self frameSetter];
    self.playerView.frame = [UIScreen mainScreen].bounds;
}


@end
